const jwt = require('jsonwebtoken')
const { secret } = require('../config/config')
module.exports = (req, res, next) => {
  // let accounts = db.get('accounts').value()
  const token = req.get('token')
  if (!token) {
    return res.json({
      code: '0001',
      msg: 'token缺失',
      data: null
    })
  }
  jwt.verify(token, secret, (err, data) => {
    if (err) {
      return res.json({
        code: '0001',
        msg: 'token校验失败',
        data: null
      })
    }
    // 保存
    req.user = data
  })
  next()
}
